package com.ibm.cps.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.google.common.base.Throwables;
import com.ibm.cps.model.ServletUtil;
import com.ibm.util.ErrorCode;
import com.ibm.util.JSONUtility;
import com.ibm.util.LocalConfig;
import com.ibm.util.exception.CPSException;

public class Processors extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger;
	private ServletTemplate implementation;

	private static Processors processors = null;

	public static Processors getInstance() throws ServletException {
		if (processors == null) {
			processors = new Processors();
			processors.init();
		}
		return processors;
	}

	@Override
	public void init() throws ServletException {
		logger = Logger.getLogger(Processors.class);
		try {
			PropertyConfigurator.configure(LocalConfig.getLoggerProperties());
		} catch (CPSException e) {
			e.printStackTrace();
			return;
		}
		try {
			implementation = new ProcessorsImplementationV2();
		} catch (CPSException e) {
			logger.error(Throwables.getStackTraceAsString(e));
		}
	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String retMsg = ServletUtil.constructReturnMsg(
				ErrorCode.INTERNAL_ERROR_CODE,
				"Internal error happens, please refer to log.");
		try {
			String input = ServletUtil.getInputArguments(req.getInputStream());
			Map<String, String> keyValue = ServletUtil.getParameters(req);
			if (input != null && input.length() >= 1) {
				HashMap<String, String> jsonKeyvalue = JSONUtility
						.getKeyValue(input);
				keyValue.putAll(jsonKeyvalue);
			}
			retMsg = implementation.doDelete(keyValue);
		} catch (Throwable e) {
			logger.error(Throwables.getStackTraceAsString(e));
			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = resp.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String retMsg = null;
		try {
			Map<String, String> key2Value = ServletUtil.getParameters(req);
			retMsg = implementation.doGet(key2Value);
		} catch (Throwable e) {
			logger.error(Throwables.getStackTraceAsString(e));
			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = resp.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}

	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String retMsg = null;
		try {
			Map<String, String> key2Value = ServletUtil.getParameters(req);
			String input = ServletUtil.getInputArguments(req.getInputStream());
			retMsg = implementation.doPut(key2Value, input);
		} catch (Throwable e) {
			logger.error(Throwables.getStackTraceAsString(e));
			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = resp.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		String retMsg = null;
		try {
			Map<String, String> key2Value = ServletUtil.getParameters(req);
			String input = ServletUtil.getInputArguments(req.getInputStream());
			retMsg = implementation.doPost(key2Value, input);
		} catch (Throwable e) {
			logger.error(Throwables.getStackTraceAsString(e));
			retMsg = ServletUtil.getException(e);
		} finally {
			PrintWriter writer = response.getWriter();
			writer.write(retMsg);
			writer.close();
		}
	}

}
